package com.anysoft.ui.activity.web;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.webkit.WebView;

import com.anysoft.base.BaseWebViewActivity;
import com.anysoft.callback.StringDialogCallback;
import com.anysoft.config.AppConstant;
import com.anysoft.model.BaseEntity;
import com.anysoft.utils.ActivityCollector;
import com.anysoft.utils.FileChooseUtil;
import com.anysoft.utils.HttpUtil;
import com.anysoft.utils.NetWorkUtil;
import com.anysoft.utils.StringUtil;
import com.anysoft.utils.ToastUtil;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.util.HashMap;

/**
 * Created by lty on 2018/6/26.
 */
public class MsMentorInfoEditActivity extends BaseWebViewActivity {
    
    private String fileName;
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String titleName = getIntent().getStringExtra("titleName");
        String params = getIntent().getStringExtra("params");
        title.setText(StringUtil.isEmpty(titleName) ? "" : titleName);
        if (StringUtil.isEmpty(params)) {
            loadUrl = generateUrl("msMentorInfoEdit", HttpUtil.getCommonParams2StringByWeb(this));
        } else {
            loadUrl = generateUrl("msMentorInfoEdit", HttpUtil.getCommonParams2StringByWeb(this) + "&" + params);
        }
        if (NetWorkUtil.isConnect(this)) {
            setWebViewConfig(loadUrl);
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    }
    
    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String url, String params) {
        HashMap<String, String> urlMap = handleUrlParams(url);
        String titleName = urlMap.get("title");
        if (url.contains("msLoginUserEdit.html")) {  //维护父母登录信息
            mIntent.putExtra("titleName", titleName);
            mIntent.putExtra("params", params);
            ActivityCollector.startTargetActivity(this, MsLoginUserEditActivity.class, mIntent);
        } else {
            webview.loadUrl(url);
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {//选择文件返回
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case AppConstant.REQUEST_CHOOSEFILE:
                    Uri uri = data.getData();
                    String chooseFilePath = FileChooseUtil.getInstance(this).getChooseFileResultPath(uri);
                    uploadFile(chooseFilePath);
                    break;
            }
        }
    }
    
    private void uploadFile(String chooseFilePath) {
        if (TextUtils.isEmpty(chooseFilePath) || TextUtils.isEmpty(uploadUrl)) {
            return;
        }
        File filePath = new File(chooseFilePath);
        if (!filePath.exists()) {
            ToastUtil.showShort("文件不存在，请修改文件路径");
            return;
        }
        String token = (TextUtils.isEmpty(AppConstant.token)) ? "" : AppConstant.token;
        fileName = filePath.getName();
        OkGo.<String>post(uploadUrl)
                .tag(this)
                .params("file", filePath)
                .params("token", token)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseResponse(response.body());
                    }
                    
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showShort("上传失败，请重试!");
                    }
                    
                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }
    
    private void parseResponse(String json) {
        BaseEntity baseEntity = null;
        try {
            baseEntity = new Gson().fromJson(json, BaseEntity.class);
            if ("0".equals(baseEntity.getCode())) {
                mWebView.loadUrl("javascript:uploadCallBack('" + fileName + "')");
            } else {
                ToastUtil.showShort("上传" + baseEntity.getMsg());
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
